package com.lin.cloud.disk.controller;

import com.lin.cloud.disk.common.constant.RedisConstant;
import com.lin.cloud.disk.common.entity.request.AccountRequestDTO;
import com.lin.cloud.disk.common.enums.EDeleteStatus;
import com.lin.cloud.disk.common.enums.EUploadStatus;
import com.lin.cloud.disk.common.exception.BusinessException;
import com.lin.cloud.disk.common.utils.Result;
import com.lin.cloud.disk.entity.dto.DirectoryAndFileDTO;
import com.lin.cloud.disk.entity.pojo.File;
import com.lin.cloud.disk.entity.request.directory.MoveDirectoryRequest;
import com.lin.cloud.disk.entity.request.directory.RestoreDirectoryRequest;
import com.lin.cloud.disk.entity.request.directory.UpdateDirectoryNameRequest;
import com.lin.cloud.disk.entity.request.file.*;
import com.lin.cloud.disk.entity.response.*;
import com.lin.cloud.disk.manage.IFileManageService;
import com.lin.cloud.disk.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 文件控制器
 *
 * @author ljc
 * @date 2021/2/3 16:41
 */
@RestController
@RequestMapping("/api/file")
@Slf4j
@Api(tags = "文件服务类")
public class FileController {

    @Resource
    private IFileManageService fileManageService;

    @Resource
    private IFileService fileService;

    @PostMapping(value = "/getPreUploadUrl")
    @ApiOperation(value = "获取预上传地址")
    public Result<GetPreUploadUrlResponse> getPreUploadUrl(@RequestBody @Validated GetPreUploadUrlRequest request) {
        return Result.success(fileManageService.getPreUploadUrl(request));
    }

    @PostMapping(value = "/getMultipartUploadUrl")
    @ApiOperation(value = "获取上传地址列表")
    public Result<GetMultipartUploadUrlResponse> getMultipartUploadUrl(@RequestBody @Validated GetMultipartUploadUrlRequest request) {
        return Result.success(fileManageService.getMultipartUploadUrl(request));
    }

    @PostMapping(value = "/getUploadUrl")
    @ApiOperation(value = "获取上传地址列表")
    public Result<GetUploadUrlResponse> getUploadUrl(@RequestBody @Validated GetUploadUrlRequest request) {
        return Result.success(fileManageService.getUploadUrl(request));
    }

    @PutMapping(value = "/mergeFilePart")
    @ApiOperation(value = "上传完成后 合并多个文件")
    public Result mergeFilePart(@RequestBody @Validated MergeFilePartRequest request) {
        fileManageService.mergeFilePart(request);
        return Result.success();
    }


    @PostMapping(value = "/getBeingUploadFile")
    @ApiOperation(value = "获取用户未上传完成的文件")
    public Result<List<GetBeingUploadFileResponse>> getBeingUploadFile(@RequestBody @Validated GetBeingUploadFileRequest request) {
        return Result.success(fileService.getBeingUploadFile(request));
    }


    @PostMapping(value = "/getFileListByFileType")
    @ApiOperation(value = "通过文件类型获取文件列表")
    public Result<List<DirectoryAndFileDTO>> getFileListByFileType(@RequestBody @Validated GetFileListByFileTypeRequest request) {
        return Result.success(fileService.getFileListByFileType(request));
    }


    /**
     * 逻辑删除文件
     *
     * @param request
     */
    @DeleteMapping(value = "/logicRemoveFile")
    @ApiOperation(value = "逻辑删除文件")
    public Result logicRemoveFile(@RequestBody @Validated RemoveFileRequest request) {
        fileManageService.logicRemoveFile(request);
        return Result.success();
    }

    /**
     * 逻辑恢复文件
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/logicRestoreFile")
    @ApiOperation(value = "逻辑恢复文件")
    public Result logicRestoreFile(@RequestBody @Validated RestoreDirectoryRequest request) {
        fileManageService.logicRestoreFile(request);
        return Result.success();
    }


    /**
     * 物理删除文件
     *
     * @param request
     */
    @DeleteMapping(value = "/physicalRemoveFile")
    @ApiOperation(value = "物理删除文件")
    public Result physicalRemoveFile(@RequestBody @Validated RemoveFileRequest request) {
        fileManageService.physicalRemoveFile(request);
        return Result.success();
    }


    /**
     * 获取直链下载链接 7天限制
     *
     * @param request
     */
    @PostMapping(value = "/getStraightDownloadUrl")
    @ApiOperation(value = "获取直链下载链接 可选择分享下载(7天限制) 或是 直接下载")
    public Result<GetFileUrlResponse> getStraightDownloadUrl(@RequestBody @Validated GetStraightDownloadUrlRequest request) {
        return Result.success(fileManageService.getStraightDownloadUrl(request));
    }

    /**
     * 获取文件地址
     *
     * @param request
     */
    @PostMapping(value = "/getFileUrl")
    @ApiOperation(value = "获取文件地址")
    public Result<GetFileUrlResponse> getFileUrl(@RequestBody @Validated GetFileUrlRequest request) {
        return Result.success(fileManageService.getFileUrl(request));
    }

    @PutMapping(value = "/updateFileName")
    @ApiOperation(value = "修改文件名字")
    public Result updateFileName(@RequestBody @Validated UpdateFileNameRequest request) {
        fileManageService.updateFileName(request);
        return Result.success();
    }

    @PutMapping(value = "/moveFile")
    @ApiOperation(value = "移动文件")
    public Result moveFile(@RequestBody @Validated MoveFileRequest request) {
        fileManageService.moveFile(request);
        return Result.success();
    }

    @PostMapping(value = "/searchFile")
    @ApiOperation(value = "搜索文件")
    public Result<List<DirectoryAndFileDTO>> searchFile(@RequestBody @Validated SearchFileRequest request){
        return Result.success(fileService.searchFile(request));
    }


    @PostMapping(value = "/uploadImg")
    @ApiOperation(value = "上传图片")
    public Result<UploadImgResponse> uploadImg(@RequestParam("file") MultipartFile file){
        return Result.success(fileManageService.uploadImg(file));
    }



}
